<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="handleCancel"
    cancelText="关闭"
    okText="排单"
    centered
  >
    <CamelliaMesProcessPlanForm ref="registerForm" @ok="submitCallback" :formDisabled="disableSubmit" :formBpm="false"></CamelliaMesProcessPlanForm>
  </a-modal>
</template>

<script lang="ts" setup>
import responsiveObserve from 'ant-design-vue/lib/_util/responsiveObserve';
import { ref, nextTick, defineExpose } from 'vue';
import CamelliaMesProcessPlanForm from './CamelliaMesProcessPlanForm.vue';
import { defHttp } from '/@/utils/http/axios';

const title = ref<string>('');
const width = ref<number>(800);
const visible = ref<boolean>(false);
const disableSubmit = ref<boolean>(false);
const registerForm = ref();
const emit = defineEmits(['register', 'success']);

/**
 * 编辑
 * @param record
 */
function edit(record) {
  title.value = disableSubmit.value ? '生产详情' : '排单';
  visible.value = true;
  let params = { orderId: record.id };
  let url = '/mes/camelliaMesProcessPlan/all_enable';

  defHttp.get({ url, params }).then((respose) => {
    let machines = [];
    for(let item of respose){
      let machine = {'value': item.id, 'label': item.deviceName}
      machines.push(machine)
    }
    
    url = '/mes/camelliaMesProcessPlan/queryByOrderId';
    defHttp.get({ url, params }).then((res) => {
      let data = res;
      data.machinesOptions = machines
    
      nextTick(() => {
        registerForm.value.edit(data);
      });
    });
  });
}

/**
 * 确定按钮点击事件
 */
function handleOk() {
  registerForm.value.submitForm();
}

/**
 * form保存回调事件
 */
function submitCallback() {
  handleCancel();
  emit('success');
}

/**
 * 取消按钮回调事件
 */
function handleCancel() {
  visible.value = false;
}

defineExpose({
  edit,
  disableSubmit,
});
</script>

<style>
/**隐藏样式-modal确定按钮 */
.jee-hidden {
  display: none !important;
}
</style>
